const {
  adminLogin,
  findAllUserInfo,
  findAllCollect,
} = require("../service/admin.service");

class AdminController {
  async login(ctx, next) {
    const { user_name, password } = ctx.request.body;

    //*1. 获取用户信息(在token的payload中, 记录id, user_name)
    try {
      //! 从返回结果对象中剔除 password 属性, 将剩下的属性放到res对象中
      const res = await adminLogin(user_name, password);

      //? ctx.body = `欢迎回来, ${user_name}`;

      if (res) {
        ctx.body = {
          code: 0,
          message: "管理员登录成功",
          result: "",
        };
      } else {
        ctx.body = {
          code: 1,
          message: "登录失败!",
          result: "",
        };
      }
    } catch (err) {
      console.error("管理员登录失败", error);
    }
  }

  async getAllUserInfo(ctx, next) {
    try {
      //! 从返回结果对象中剔除 password 属性, 将剩下的属性放到res对象中
      const res = await findAllUserInfo();

      //? ctx.body = `欢迎回来, ${user_name}`;

      if (res) {
        ctx.body = {
          code: 0,
          message: "获取所有用户基本信息成功",
          result: res,
        };
      } else {
        ctx.body = {
          code: 1,
          message: "获取所有用户基本信息失败!",
          result: "",
        };
      }
    } catch (err) {
      console.error("管理员获取所有用户信息失败", error);
    }
  }

  async getAllCollect(ctx) {
    try {
      //! 从返回结果对象中剔除 password 属性, 将剩下的属性放到res对象中
      const collect_arr = await findAllCollect();
      console.log("-------------------------------------");
      console.log(collect_arr);
      console.log("+++++++++++++++++++++++++++++++++++++");
      if (collect_arr) {
        ctx.body = {
          code: 0,
          message: "获取所有用户的收藏成功",
          result: collect_arr,
        };
      } else {
        ctx.body = {
          code: 1,
          message: "获取所有用户的收藏失败!",
          result: "",
        };
      }
    } catch (err) {
      console.error("管理员获取所有用户收藏失败", error);
    }
  }
}

module.exports = new AdminController();
